package com.video.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.video.manage.base.Result;
import com.video.manage.entity.EnjoyCollectionEntity;
import com.video.manage.entity.EvaluationEntity;
import com.video.manage.entity.VideoEntity;
import com.video.manage.mapper.EnjoyCollectionDao;
import com.video.manage.mapper.EvaluationDao;
import com.video.manage.mapper.VideoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class VideoController {
    @Autowired
    private VideoDao videoDao;

    @Autowired
    private EvaluationDao evaluationDao;

    @Autowired
    private EnjoyCollectionDao enjoyCollectionDao;

    /**
     * 视频列表
     *
     * @return
     */
    @GetMapping("/video/list")
    public Result<VideoEntity> list(Integer type) {
        Result result = new Result();
        result.setCode(200);
        try {
            List<VideoEntity> list = videoDao.selectVideoList(type);
            result.setList(list);
        } catch (Exception e) {
            log.error("VideoController-list-error:", e);
            result.setCode(500);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 评论列表
     *
     * @return
     */
    @GetMapping("/video/evaluationList")
    public Result<EvaluationEntity> evaluationList(Integer videoId) {
        Result result = new Result();
        result.setCode(200);
        try {
            List<EvaluationEntity> list = evaluationDao.selectEvaluationList(videoId);
            result.setList(list);
        } catch (Exception e) {
            log.error("VideoController-list-error:", e);
            result.setCode(500);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 保存视频
     *
     * @param dto
     * @return
     */
    @PostMapping("/video/add")
    public Result<VideoEntity> click(@RequestBody VideoEntity dto) {
        Result result = new Result();
        result.setCode(200);
        try {
            VideoEntity videoEntity = new VideoEntity();
            videoEntity.setTitle(dto.getTitle());
            videoEntity.setContent(dto.getContent());
            videoEntity.setUrl(dto.getUrl());
            videoEntity.setType(dto.getType());
            videoEntity.setCreateTime(new Date());
            videoDao.insert(videoEntity);
            result.setEntity(videoEntity);
        } catch (Exception e) {
            log.error("视频保存成功：", e);
            result.setCode(500);
            result.setMessage("视频保存失败");
        }
        return result;
    }

    /**
     * 点赞
     */
    @PostMapping("/video/click")
    public Result<Void> click(@RequestBody EnjoyCollectionEntity dto) {
        Result result = new Result();
        result.setCode(200);
        try {
            int count = enjoyCollectionDao.click(dto.getVideoId(), dto.getUserId(), dto.getClick());
            if (count <= 0) {
                EnjoyCollectionEntity entity = new EnjoyCollectionEntity();
                entity.setClick(dto.getClick());
                entity.setUserId(dto.getUserId());
                entity.setVideoId(dto.getVideoId());
                entity.setCreateTime(new Date());
                enjoyCollectionDao.insert(entity);
            }
            VideoEntity videoEntity = videoDao.selectById(dto.getVideoId());
            videoEntity.setCTotal(dto.getClick() == 1 ? videoEntity.getCTotal() + 1 : videoEntity.getCTotal() - 1);
            videoDao.updateById(videoEntity);
        } catch (Exception e) {
            log.error("点赞成功：", e);
            result.setCode(500);
            result.setMessage("点赞失败");
        }
        return result;
    }

    /**
     * 收藏
     */
    @PostMapping("/video/store")
    public Result<Void> store(@RequestBody EnjoyCollectionEntity dto) {
        Result result = new Result();
        result.setCode(200);
        try {
            int count = enjoyCollectionDao.store(dto.getVideoId(), dto.getUserId(), dto.getStore());
            if (count <= 0) {
                EnjoyCollectionEntity entity = new EnjoyCollectionEntity();
                entity.setStore(dto.getStore());
                entity.setUserId(dto.getUserId());
                entity.setVideoId(dto.getVideoId());
                entity.setCreateTime(new Date());
                enjoyCollectionDao.insert(entity);
            }
            VideoEntity videoEntity = videoDao.selectById(dto.getVideoId());
            videoEntity.setSTotal(dto.getStore() == 1 ? videoEntity.getSTotal() + 1 : videoEntity.getSTotal() - 1);
            videoDao.updateById(videoEntity);
        } catch (Exception e) {
            log.error("收藏成功：", e);
            result.setCode(500);
            result.setMessage("收藏失败");
        }
        return result;
    }

    /**
     * 评论
     */
    @PostMapping("/video/comment")
    public Result<Void> comment(@RequestBody EvaluationEntity dto) {
        Result result = new Result();
        result.setCode(200);
        try {
            EvaluationEntity entity = new EvaluationEntity();
            entity.setComment(dto.getComment());
            entity.setUserId(dto.getUserId());
            entity.setVideoId(dto.getVideoId());
            entity.setCreateTime(new Date());
            evaluationDao.insert(entity);
            VideoEntity videoEntity = videoDao.selectById(dto.getVideoId());
            videoEntity.setPTotal(videoEntity.getPTotal() + 1);
            videoDao.updateById(videoEntity);
        } catch (Exception e) {
            log.error("评论成功：", e);
            result.setCode(500);
            result.setMessage("评论失败");
        }
        return result;
    }

    /**
     * 查询视频
     *
     * @param key
     * @return
     */
    @GetMapping("/video/key")
    public Result<VideoEntity> list(String key) {
        Result result = new Result();
        result.setCode(200);
        try {
            QueryWrapper<VideoEntity> wrapper = new QueryWrapper<>();
            wrapper.like("title", key);
            List<VideoEntity> list = videoDao.selectList(wrapper);
            result.setList(list);
        } catch (Exception e) {
            log.error("VideoController-list-error:", e);
            result.setCode(500);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 我的视频收藏列表
     */
    @GetMapping("/video/myList")
    public Result<VideoEntity> myList(Integer userId) {
        Result result = new Result();
        result.setCode(200);
        try {
            List<VideoEntity> list = videoDao.selectMyList(userId);
            result.setList(list);
        } catch (Exception e) {
            log.error("VideoController-list-error:", e);
            result.setCode(500);
            result.setMessage("查询失败");
        }
        return result;
    }
}
